function ProductEditor()
{
	this.count=0;
	this.button_obj=false;	
	this.win_obj=false;
	this.selected_catalogs=[];
	this.last_selected_category=false;
	/*Вызов окна выбора каталогов*/
	this.getCatalogList=function(button_obj)
	{
		this.button_obj=$(button_obj);
		form=this.button_obj.parents('.form');
		selected_category=form.find('.category_id').val();
		selected_catalogs=[];
		count=0;
		form.find('ul.selected.catalogs li input:hidden').each(function(){
			selected_catalogs[count++]=$(this).val();
		});
		this.selected_catalogs=selected_catalogs;
		$.post('/admin/?ajax=store_product_catalogs',{'parents':'#'+selected_category+'#','selected_catalogs':selected_catalogs},this.showCatalog);
	};
	this.showCatalog=function(data)
	{
		product_editor.win_obj=$().modal_win({'cont':'<div class="parent_cont">'+data+'</div>','title':'Выберите каталог','width':'500px','height':'600px','delete_on_close':true,'t_class':'select_parents'},true);
		stopLoading();
	};
	this.updateList=function(parents,obj)
	{
		$.post('/admin/?ajax=store_product_catalogs',{'parents':parents,'selected_catalogs':this.selected_catalogs},this.refreshList);
	};
	this.refreshList=function(data)
	{
		product_editor.win_obj.find('.parent_cont').html(data);		
	};
	this.addCatalog=function(desc,id,obj)
	{
		select_s=this.button_obj.parents('.field').find('ul.selected');
		select_s.append('<li title="'+desc+'"><div class="delete" onclick="product_editor.delete_(this)">x</div><input type="hidden" name="product[pages][c-'+(this.count++)+']" value="'+id+'" />'+$(obj).text()+'</li>').end().find('.count span:not(.delete)').text(select_s.find('li:not(.hidden)').length);
		dee_modal_win.close();
	};
	this.delete_=function(obj)
	{
		obj=$(obj);
		field=obj.parents('.field');
		obj.parent().remove();
		field.find('.count span').html(field.find('ul.selected li:not(.hidden)').length);
	};
	this.onChangeCatalog=function(obj)
	{
		obj=$(obj);
		form=obj.parents('.product_edit');
		catalog_list=form.find('.catalogs_list');
		if((catalog_list.find('ul.selected li').length ||form.find('.apply_to_pages label').length)&& !confirm('Если вы измените категорию придется удалить все страницы товара из других категорий'))
		return obj.val(this.last_selected_category);
			catalog_list
				.find('ul.selected li').remove().end()
				.find('.count span').text(0);
	};
	this.beforeChangeCatalog=function(obj)
	{
		this.last_selected_category=$(obj).val();
	};
}

var product_editor=new ProductEditor();

function IdSelector()
{
	this.input=false;
	this.ajax_link=false;
	this.win_obj=false;
	this.show=function(obj,ajax_file)
	{
		obj=$(obj);
		if(obj.hasClass('disabled'))return;
		this.input=obj;
		this.ajax_link='/admin/?ajax='+ajax_file;
		$.post(this.ajax_link,{'action':'view'},this.viewWind);		
	};
	/*Показать окно*/
	this.viewWind=function(data)
	{
		eval(data);
		_id_selector.win_obj=$().modal_win({'cont':'<div class="parent_cont">'+data.html+'</div>','title':data.title,'width':'500px','height':'600px','delete_on_close':true,'t_class':'select_parents'},true);
	};
	/*Выбрано промежуточное категория*/
	this.updateList=function(params)
	{
		$.post(this.ajax_link,$.extend({'action':'update'},params),this.reviewList);		
	};
	this.reviewList=function(data)
	{
		eval(data);
		_id_selector.win_obj.find('.parent_cont').html(data.html);
	};
	this.select=function(id,title)
	{
		this.input.val(title).parent().find('.spec_hidden').val(id);
		dee_modal_win.close();
	}
}
var _id_selector=new IdSelector();

function addCharacteristics(obj,field_id)
{
	par=$(obj).parent().find('input');
	$.post(_id_selector.ajax_link,{'action':'add','field_id':field_id,'value':par.filter('._value').val(),'value_sys':par.filter('._value_sys').val()},_id_selector.reviewList);
}
